package com.bootcamp.ex1125.config;

import com.bootcamp.ex1125.interceptor.UserHelloInterceptor;
import com.bootcamp.ex1125.interceptor.UserLoginInterceptor;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: spring-1125-mvc-java-config
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-25 11:18
 **/


/*
* 以 MVC开头标签的配置：
*   如：validator，interceptor，resources，convertors
*    实现的接口 可以实现上述配置
* */

@ComponentScan("com.bootcamp.ex1125.controller")
@EnableWebMvc
/* 因为 Spring配置类派出了 controller，所以注解写在 mvc配置文件*/
@EnableAspectJAutoProxy
public class SpringMvcConfiguration implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserHelloInterceptor()).addPathPatterns("/user/hello");
        registry.addInterceptor(new UserLoginInterceptor()).addPathPatterns("/user/login");
    }

    /* 添加 validator*/
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setProviderClass(HibernateValidator.class);
        return validator;
    }


}
